package com.etonedu.meals.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.etonedu.meals.R;
import com.etonedu.meals.common.SPConst;
import com.etonedu.meals.utils.SharedPreferencesUtil;
import com.etonedu.meals.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

public class HttpUrlDialog extends Dialog {
    private Context context;
    public HttpUrlDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public HttpUrlDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //提前设置Dialog的一些样式
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);//设置dialog显示居中
        //dialogWindow.setWindowAnimations();设置动画效果
        setContentView(R.layout.dialog_httpurl);

        WindowManager windowManager = ((Activity)context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = display.getWidth()*2/5+100;// 设置dialog宽度为屏幕的4/5
        lp.height = display.getHeight() * 1/3+100;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);//点击外部Dialog消失

        SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());

        Spinner spinner = findViewById(R.id.sp_http);
        EditText et_http_addr = findViewById(R.id.et_http_addr);
        EditText et_city_code = findViewById(R.id.et_city_code);

        String[] https = context.getResources().getStringArray(R.array.http_url);

        HttpsAdapter adapter = new HttpsAdapter(context,https);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String http = https[position];
                LogUtils.e(http);
                if ("测试".equals(http)) {
                    et_http_addr.setText("http://47.106.165.234:8181/api");
                } else if ("生产".equals(http)) {
//                    et_http_addr.setText("http://agw.woniukid.cn/api");
                    et_http_addr.setText("http://123.207.103.39:8080/api");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        String url = spu.getString(SPConst.HOST_URL);
        if (!Utils.isEmpty(url)) {
            for (int i = 0 ; i < https.length ; i++) {
                String result = https[i];

                if ("http://47.106.165.234:8181/api".equals(url)) {
                    String content = "测试";
                    if (content.equals(result)) {
                        et_http_addr.setText("http://47.106.165.234:8181/api");
                        spinner.setSelection(1,true);
                    }
                } else if ("http://123.207.103.39:8080/api".equals(url)) {
                    String content = "生产";
                    if (content.equals(result)) {
                        et_http_addr.setText("http://123.207.103.39:8080/api");
                        spinner.setSelection(0,true);
                    }
                }
            }
        }

        Button button = findViewById(R.id.http_button_close);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                spu.clear();
                String url = et_http_addr.getText().toString();
                spu.setString(SPConst.HOST_URL,url);

                String cityCode = et_city_code.getText().toString();

                spu.setString(SPConst.CITY_CODE,cityCode);

                EventBus.getDefault().post("切换环境");

                dismiss();
            }
        });

        Button clearButton = findViewById(R.id.http_button_clear);
        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                spu.clear();
            }
        });
    }

    private class HttpsAdapter extends BaseAdapter {

        private String[] items;
        private Context context;
        public HttpsAdapter(Context context, String[] items) {
            this.context = context;
            this.items = items;
        }
        @Override
        public int getCount() {
            return items != null ? items.length : 0;
        }

        @Override
        public Object getItem(int position) {
            return items != null ? items[position] : "";
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(context).inflate(android.R.layout.simple_spinner_item,null);
            view.setBackgroundColor(Color.TRANSPARENT);
            TextView textView = view.findViewById(android.R.id.text1);
            textView.setTextColor(Color.BLACK);

            view.setPadding(10,10,10,10);

            textView.setText(items[position]);
            return view;
        }
    }

}
